package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.pojo.DishNew;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.utils.PojoDataExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 10:38
 * @Desc:
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryService categoryService;

    @Override
    public Page<DishNew> listAll(Integer page, Integer pageSize, String name) {
        Page<Dish> page1 = new Page<>(page, pageSize);

        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", "%".concat(name).concat("%"));
        }
        wrapper.orderByDesc("update_time");

        Page<Dish> dishPage = dishMapper.selectPage(page1, wrapper);

        Page<DishNew> dishNewPage = new Page<>(page, pageSize);
        dishNewPage.setTotal(dishPage.getTotal());

        ArrayList<DishNew> dishNews = new ArrayList<>();
        //修改菜品分类，以名字展示
        List<Dish> records = dishPage.getRecords();
        for (Dish record : records) {
            //将菜品信息封装到新的实体类
            String categoryName = categoryService.findCategoryById(record.getCategoryId());
            DishNew dishNew = PojoDataExchange.dataExchange(record, categoryName);

            dishNews.add(dishNew);
        }

        return dishNewPage.setRecords(dishNews);
    }

    @Override
    public int deleteDish(Long[] ids) {
        for (Long id : ids) {
            int i = dishMapper.deleteById(id);

            if (i == 0) i = 1 / 0;
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDishStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            DishNew dish = new DishNew();
            dish.setStatus(status);
            dish.setId(id);
            int i = dishMapper.updateById(dish);

            if (i == 0) i = 1 / 0;
        }

        return 1;
    }

    @Override
    public int addDish(DishNew dish) {
        return dishMapper.insert(dish);
    }

    @Override
    public DishNew findDishById(Long id) {
        Dish dish = dishMapper.selectById(id);

        //将信息封装到新实体类中
        String categoryName = categoryService.findCategoryById(dish.getCategoryId());
        DishNew dishNew = PojoDataExchange.dataExchange(dish, categoryName);

        dishNew.setFlavors(findDishFlavor(id));

        return dishNew;
    }

    @Override
    public List<DishFlavor> findDishFlavor(Long id) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);

        List<DishFlavor> flavors = dishFlavorMapper.selectList(wrapper);

        return flavors;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDishFlavorInfo(List<DishFlavor> dishFlavors) {
        for (DishFlavor dishFlavor : dishFlavors) {
            int i = dishFlavorMapper.updateById(dishFlavor);

            if (i == 0) i = 1 / 0;
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDishInfo(DishNew dishNew) {
        Dish dish = PojoDataExchange.backExchange(dishNew);

        int i = dishMapper.updateById(dish);

        if (i != 0) {
            return updateDishFlavorInfo(dishNew.getFlavors());
        }

        return 0;
    }

    @Override
    public List<Dish> findDishByCaregoryId(Long categoryId, String name, Integer status) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();

        if (categoryId != null) wrapper.eq("category_id", categoryId);
        if (name != null) wrapper.eq("name", name);
        if (status != null) wrapper.eq("status", status);

        return dishMapper.selectList(wrapper);
    }
}
